﻿using System;
using System.ComponentModel;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using EIIS.Infrastructure.Service.Business;
using EIIS.Infrastructure.Service.System;
using EIIS.Infrastructure.Model.Business;
using EIIS.Infrastructure.Model.System;
using EIIS.Presentation.Controllers.CommonSupport;
using EIIS.Presentation.Controllers.ViewModel;
using EIIS.Common.Jsons;
using EIIS.FrameWork.Attributes;
using System.Web.Script;
using EIIS.Infrastructure.Model.System.Authority;
using EIIS.FrameWork.Cache;

namespace EIIS.Presentation.Controllers.Business
{
    /// <summary>
    /// 数据报表
    /// </summary>
    [Description("数据报表管理")]
    public class ReportController : BaseController
    {
        public IDictionaryManager DictionaryManager { get; set; }
        public IPackageManager PackageManager { get; set; }
        public ICache Cache { get; set; }
        private DateTime tempDate = DateTime.Parse("0001-1-1 0:00:00");

        string[] ReportColors = new string[] { "#4572A7", "#AA4643", "#89A54E", "#80699B", "#3D96AE", "#DB843D", "#92A8CD", "#A47D7C", "#B5CA92" };

        [Description("今日详情页面")]
        [ViewPage]
        public ActionResult DayReport()
        {
            return View();
        }

        [Description("库存物品统计页面")]
        [ViewPage]
        public ActionResult InventoryReport()
        {
            return View();
        }

        [Description("时间段详情页面")]
        [ViewPage]
        public ActionResult PeriodReport()
        {
            return View();
        }

        [Description("流程统计分析页面")]
        [ViewPage]
        public ActionResult PeriodReport1()
        {
            return View();
        }

        [Description("灭菌锅使用统计页面")]
        [ViewPage]
        public ActionResult MieJGReport()
        {
            return View();
        }

        [Description("清洗器使用统计页面")]
        [ViewPage]
        public ActionResult QingXGReport()
        {
            return View();
        }

        [Description("工作量统计页面")]
        [ViewPage]
        public ActionResult WorkLoadReport()
        {
            return View();
        }

        //加载今日详情
        public ActionResult LoadDayReport()
        {
            string strDay = DateTime.Now.ToString("yyyy-MM-dd");
            IEnumerable<V_Package> list = PackageManager.GetAllPackageView().Where(p => p.BaoZRQ == strDay || p.MieJRQ == strDay || p.FaFRQ == strDay || p.HuiSRQ == strDay);
            long BaoZ_Count = 0;
            long MieJ_Count = 0;
            long FaF_Count = 0;
            long HuiS_Count = 0;
            BaoZ_Count = list.Where(p =>
            {
                string baozsj = p.BaoZSJ;
                if (!string.IsNullOrEmpty(baozsj))
                {
                    baozsj = baozsj.Substring(0, 10);
                }
                return DateTime.Now.ToString("yyyy-MM-dd") == baozsj;
            }).LongCount();
            MieJ_Count = list.Where(p =>
            {
                string miejsj = p.MieJSJ;
                if (!string.IsNullOrEmpty(miejsj))
                {
                    miejsj = miejsj.Substring(0, 10);
                }
                return DateTime.Now.ToString("yyyy-MM-dd") == miejsj;
            }).LongCount();
            FaF_Count = list.Where(p =>
            {
                string fafsj = p.FaFSJ;
                if (!string.IsNullOrEmpty(fafsj))
                {
                    fafsj = fafsj.Substring(0, 10);
                }
                return DateTime.Now.ToString("yyyy-MM-dd") == fafsj;
            }).LongCount();
            HuiS_Count = list.Where(p =>
            {
                string huissj = p.HuiSSJ;
                if (!string.IsNullOrEmpty(huissj))
                {
                    huissj = huissj.Substring(0, 10);
                }
                return DateTime.Now.ToString("yyyy-MM-dd") == huissj;
            }).LongCount();
            List<ReportDataView> data = new List<ReportDataView>();
            data.Add(new ReportDataView() { y = BaoZ_Count, color = ReportColors[0] });
            data.Add(new ReportDataView() { y = MieJ_Count, color = ReportColors[1] });
            data.Add(new ReportDataView() { y = FaF_Count, color = ReportColors[2] });
            data.Add(new ReportDataView() { y = HuiS_Count, color = ReportColors[3] });
            ReportView reportView = new ReportView()
            {
                name = "今日详情",
                data = data,
                color = "white"
            };
            List<ReportView> reportViews = new List<ReportView>();
            reportViews.Add(reportView);

            string[] categoryData = new string[] { "包装", "灭菌", "发放", "回收" };

            return this.JsonFormat(new { color = ReportColors, category = categoryData, series = reportViews });
        }

        //加载库存物品统计
        public ActionResult LoadInventoryReport()
        {
            IList query = PackageManager.GetInventoryReport();
            string[] categoryData = new string[query.Count];
            List<ReportDataView> data = new List<ReportDataView>();
            int j = 0;
            for (int i = 0; i < query.Count; i++)
            {
                object[] objs = (object[])query[i];
                categoryData[i] = objs[0].ToString();
                if (i <= 8) j = i;
                else j = i % 8;
                data.Add(new ReportDataView() { y = Convert.ToInt32(objs[1]), color = ReportColors[j] });
            }
            ReportView reportView = new ReportView()
            {
                name = "库存物品统计",
                data = data,
                color = "white"
            };
            List<ReportView> reportViews = new List<ReportView>();
            reportViews.Add(reportView);
            return this.JsonFormat(new { color = ReportColors, category = categoryData, series = reportViews });
        }

        //加载时间段详情
        public ActionResult LoadPeriodReport()
        {
            string strBegin = Request["begin"];
            string strEnd = Request["end"];
            IList<V_Package> list = PackageManager.GetRecordsByPeriodTime(strBegin, strEnd, "");
            int BaoZ_Count = 0;
            int MieJ_Count = 0;
            int FaF_Count = 0;
            int HuiS_Count = 0;
            BaoZ_Count = list.Where(p =>
            {
                DateTime begin = tempDate;
                DateTime end = tempDate;
                DateTime baozsj = tempDate;
                if (!string.IsNullOrEmpty(strBegin)) begin = DateTime.Parse(strBegin);
                if (!string.IsNullOrEmpty(strEnd)) end = DateTime.Parse(strEnd).AddDays(1);
                if (!string.IsNullOrEmpty(p.BaoZSJ)) baozsj = DateTime.Parse(p.BaoZSJ);
                if (begin != tempDate && end != tempDate && baozsj != tempDate)
                {
                    return baozsj >= begin && baozsj < end;
                }
                else if (begin != tempDate && baozsj != tempDate)
                {
                    return baozsj >= begin;
                }
                else if (end != tempDate && baozsj != tempDate)
                {
                    return baozsj < end;
                }
                else
                {
                    return baozsj != tempDate;
                }
            }).Count();
            MieJ_Count = list.Where(p =>
            {
                DateTime begin = tempDate;
                DateTime end = tempDate;
                DateTime miejsj = tempDate;
                if (!string.IsNullOrEmpty(strBegin)) begin = DateTime.Parse(strBegin);
                if (!string.IsNullOrEmpty(strEnd)) end = DateTime.Parse(strEnd).AddDays(1);
                if (!string.IsNullOrEmpty(p.MieJSJ)) miejsj = DateTime.Parse(p.MieJSJ);
                if (begin != tempDate && end != tempDate && miejsj != tempDate)
                {
                    return miejsj >= begin && miejsj < end;
                }
                else if (begin != tempDate && miejsj != tempDate)
                {
                    return miejsj >= begin;
                }
                else if (end != tempDate && miejsj != tempDate)
                {
                    return miejsj < end;
                }
                else
                {
                    return miejsj != tempDate;
                }
            }).Count();
            FaF_Count = list.Where(p =>
            {
                DateTime begin = tempDate;
                DateTime end = tempDate;
                DateTime fafsj = tempDate;
                if (!string.IsNullOrEmpty(strBegin)) begin = DateTime.Parse(strBegin);
                if (!string.IsNullOrEmpty(strEnd)) end = DateTime.Parse(strEnd).AddDays(1);
                if (!string.IsNullOrEmpty(p.FaFSJ)) fafsj = DateTime.Parse(p.FaFSJ);
                if (begin != tempDate && end != tempDate && fafsj != tempDate)
                {
                    return fafsj >= begin && fafsj < end;
                }
                else if (begin != tempDate && fafsj != tempDate)
                {
                    return fafsj >= begin;
                }
                else if (end != tempDate && fafsj != tempDate)
                {
                    return fafsj < end;
                }
                else
                {
                    return fafsj != tempDate;
                }
            }).Count();
            HuiS_Count = list.Where(p =>
            {
                DateTime begin = tempDate;
                DateTime end = tempDate;
                DateTime huissj = tempDate;
                if (!string.IsNullOrEmpty(strBegin)) begin = DateTime.Parse(strBegin);
                if (!string.IsNullOrEmpty(strEnd)) end = DateTime.Parse(strEnd).AddDays(1);
                if (!string.IsNullOrEmpty(p.HuiSSJ)) huissj = DateTime.Parse(p.HuiSSJ);
                if (begin != tempDate && end != tempDate && huissj != tempDate)
                {
                    return huissj >= begin && huissj < end;
                }
                else if (begin != tempDate && huissj != tempDate)
                {
                    return huissj >= begin;
                }
                else if (end != tempDate && huissj != tempDate)
                {
                    return huissj < end;
                }
                else
                {
                    return huissj != tempDate;
                }
            }).Count();

            List<ReportDataView> data = new List<ReportDataView>();
            data.Add(new ReportDataView() { y = BaoZ_Count, color = ReportColors[0] });
            data.Add(new ReportDataView() { y = MieJ_Count, color = ReportColors[1] });
            data.Add(new ReportDataView() { y = FaF_Count, color = ReportColors[2] });
            data.Add(new ReportDataView() { y = HuiS_Count, color = ReportColors[3] });
            ReportView reportView = new ReportView()
            {
                name = "消毒供应中心",
                data = data,
                color = "white"
            };
            List<ReportView> reportViews = new List<ReportView>();//数据列表
            reportViews.Add(reportView);

            string[] categoryData = new string[] { "包装", "灭菌", "发放", "回收" };//X轴数组

            return this.JsonFormat(new { color = ReportColors, category = categoryData, series = reportViews });
        }

        //加载流程统计分析
        public ActionResult LoadPeriodReport1()
        {
            string strBegin = Request["begin"];
            string strEnd = Request["end"];
            
            IList<V_Package> list = PackageManager.GetRecordsByPeriodTime(strBegin, strEnd, "");
            IEnumerable<IGrouping<string, V_Package>> query = list.GroupBy(p => p.BaoBH, p => p);
            int BaoZ_Count = 0;
            int MieJ_Count = 0;
            int FaF_Count = 0;
            int HuiS_Count = 0;
            List<ReportView> reportViews = new List<ReportView>();  //数据列表
            List<string> categoryData = new List<string>();         //X轴数组
            List<ReportDataView> data1 = new List<ReportDataView>();//包装的数量
            List<ReportDataView> data2 = new List<ReportDataView>();//灭菌的数量
            List<ReportDataView> data3 = new List<ReportDataView>();//发放的数量
            List<ReportDataView> data4 = new List<ReportDataView>();//回收的数量

            foreach (IGrouping<string, V_Package> info in query)
            {
                IList<V_Package> tempList = info.ToList<V_Package>();
                string ZhuangT = tempList.FirstOrDefault().ZhuangT.ToString();
                string strName = tempList.FirstOrDefault().BaoMC;
                if (categoryData.Where(p => p.Equals(strName)).Count() <= 0)
                {
                    categoryData.Add(strName);
                }

                BaoZ_Count = tempList.Count(p => !string.IsNullOrEmpty(p.BaoZR));
                MieJ_Count = tempList.Count(p => !string.IsNullOrEmpty(p.MieJR));
                FaF_Count = tempList.Count(p => !string.IsNullOrEmpty(p.FaFR));
                HuiS_Count = tempList.Count(p => !string.IsNullOrEmpty(p.HuiSR));
               
                data1.Add(new ReportDataView() { y = BaoZ_Count, color = ReportColors[0] });
                data2.Add(new ReportDataView() { y = MieJ_Count, color = ReportColors[1] });
                data3.Add(new ReportDataView() { y = FaF_Count, color = ReportColors[2] });
                data4.Add(new ReportDataView() { y = HuiS_Count, color = ReportColors[3] });
            }

            reportViews.Add(new ReportView() { 
                name="包装",
                color = ReportColors[0],
                data = data1
            });

            reportViews.Add(new ReportView()
            {
                name = "灭菌",
                color = ReportColors[1],
                data = data2
            });

            reportViews.Add(new ReportView()
            {
                name = "发放",
                color = ReportColors[2],
                data = data3
            });

            reportViews.Add(new ReportView()
            {
                name = "回收",
                color = ReportColors[3],
                data = data4               
            });

            return this.JsonFormat(new {color = ReportColors, category = categoryData.ToArray(), series = reportViews });
        }

        //加载灭菌锅使用统计
        public ActionResult LoadMieJGReport()
        {
            IList<V_Package> list = PackageManager.GetAllPackageView();
            IEnumerable<IGrouping<string, V_Package>> query = list.GroupBy(p => p.MieJGH, p => p);
            List<ReportView> reportViews = new List<ReportView>();  //数据列表
            List<ReportDataView> data = new List<ReportDataView>(); //灭菌锅使用次数的集合
            foreach (IGrouping<string, V_Package> info in query)
            {
                IList<V_Package> tempList = info.ToList<V_Package>();
                string strName = tempList.FirstOrDefault().MieJGH;
                if (!string.IsNullOrEmpty(strName))
                {
                    data.Add(new ReportDataView()
                    {
                        name = "灭菌锅" + strName,
                        y = tempList.Count()
                    });
                }
            }
            reportViews.Add(new ReportView()
            {
                type = "pie",
                name = "使用比例",
                data = data
            });

            return this.JsonFormat(new { series = reportViews });
        }

        //加载清洗器使用统计
        public ActionResult LoadQingXGReport()
        {
            IList<V_Package> list = PackageManager.GetAllPackageView();
            IEnumerable<IGrouping<string, V_Package>> query = list.GroupBy(p => p.QingXQGH, p => p);
            List<ReportView> reportViews = new List<ReportView>();  //数据列表
            List<ReportDataView> data = new List<ReportDataView>(); //清洗器使用次数的集合
            foreach (IGrouping<string, V_Package> info in query)
            {
                IList<V_Package> tempList = info.ToList<V_Package>();
                string strName = tempList.FirstOrDefault().QingXQGH;
                if (!string.IsNullOrEmpty(strName))
                {
                    data.Add(new ReportDataView()
                    {
                        name = "清洗器" + strName,
                        y = tempList.Count()
                    });
                }
            }
            reportViews.Add(new ReportView()
            {
                type = "pie",
                name = "使用比例",
                data = data
            });

            return this.JsonFormat(new { series = reportViews });
        }

        //加载工作量统计
        public ActionResult LoadWorkLoadReport()
        {
            string strWorker = string.Empty;
            if (Cache.GetSessionCache("HospUser") != null)
            {
                HospUser user = (HospUser)Cache.GetSessionCache("HospUser");
                strWorker = user.Name;
                ViewData["Worker"] = strWorker;
            }
            string strWhere = string.Format(" (baozr = '{0}') ", strWorker);
            IList<V_Package> list = PackageManager.GetRecordsByPeriodTime("", "", strWhere);
            List<ReportView> reportViews = new List<ReportView>();  //数据列表
            List<ReportDataView> data = new List<ReportDataView>(); //工作量的集合
            string[] categoryData = new string[] { "包装", "灭菌", "发放", "回收" };//X轴数组
            data.Add(new ReportDataView()
            {
                y = list.Count,
                color = ReportColors[0]
            });

            strWhere = string.Format(" (miejr = '{0}') ", strWorker);
            list = PackageManager.GetRecordsByPeriodTime("", "", strWhere);
            data.Add(new ReportDataView()
            {
                y = list.Count,
                color = ReportColors[1]

            });

            strWhere = string.Format(" (fafr = '{0}') ", strWorker);
            list = PackageManager.GetRecordsByPeriodTime("", "", strWhere);
            data.Add(new ReportDataView()
            {
                y = list.Count,
                color = ReportColors[2]

            });

            strWhere = string.Format(" (huisr = '{0}') ", strWorker);
            list = PackageManager.GetRecordsByPeriodTime("", "", strWhere);
            data.Add(new ReportDataView()
            {
                y = list.Count,
                color = ReportColors[3]

            });

            reportViews.Add(new ReportView() { 
                name = "工作量统计",
                data = data,
                color = "white"
            });
            return this.JsonFormat(new { color = ReportColors, category = categoryData, series = reportViews, worker = strWorker });
        }

    }
}
